package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.mine.ShopCartActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 热门排行
 */
public class HotRankingActivity extends BaseActivity {
    private static final int REQUEST_CART_CODE = 7768;
    private TextView tvShopCount;//购物车数量
    private FrameLayout flHotVideo, flHotBook;//热门视频，图书
    @BindView(R.id.fl_hot_course)
    protected FrameLayout flHotCourse;//热门课程
    @BindView(R.id.ll_hot_actions)
    protected FrameLayout flHotActions;//热门活动
    private HotVideoFragment videoFragment;//视频
    private HotBookFragment bookFragment;//图书
    private ActionFragment actionFragment;//活动
    private HotCourseFragment courseFragment;//课程
    private int mShoppingNum;//购物车商品数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_ranking);
        ButterKnife.bind(this);
        setTitle("范儿热门", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_IMG, R.drawable.icon_video_detail_cart);
        RelativeLayout rlTop = findViewById(R.id.rl_top);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTop,R.color.colorFAF9F9);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String type = null;
        if (bundle != null) {
            type = bundle.getString("type");
        }

        initView();
        if ("2".equals(type)) {
            setSelection(1);
        } else {
            setSelection(0);
        }
    }

    private void initView() {
        tvShopCount = (TextView) findViewById(R.id.tv_count);
        flHotVideo = (FrameLayout) findViewById(R.id.fl_hot_video);
        flHotBook = (FrameLayout) findViewById(R.id.ll_hot_book);
        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        if (!isLogin) {
            tvShopCount.setVisibility(View.GONE);
        }
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        if (!isLogin) {
            dialogConfirm(null, null, null, null).show();
            return;
        }
        Intent intent = new Intent(this,ShopCartActivity.class);
        startActivityForResult(intent, REQUEST_CART_CODE);
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        login();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == REQUEST_CART_CODE){
            if(data != null && (data.hasExtra("result") || data.hasExtra("del"))){//购物车中操作了，购买，删除
                if(videoFragment != null){
                    videoFragment.onRefresh();
                }
                if(bookFragment != null){
                    bookFragment.onRefresh();
                }
            }
        }else if(resultCode == RESULT_OK && requestCode == REQUEST_LOGIN_CODE){
            if(videoFragment != null){
                videoFragment.onRefresh();
            }
            if(bookFragment != null){
                bookFragment.onRefresh();
            }
        }
    }

    @OnClick({R.id.fl_hot_course, R.id.fl_hot_video, R.id.ll_hot_book, R.id.ll_hot_actions})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fl_hot_course://课程
                setSelection(0);
                break;
            case R.id.fl_hot_video://视频
                setSelection(1);
                break;
            case R.id.ll_hot_book://图书
                setSelection(2);
                break;
            case R.id.ll_hot_actions://活动
                setSelection(3);
                break;
        }
    }

    private void setSelection(int index) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0://
                setSelected(flHotCourse);
                if (courseFragment == null) {
                    courseFragment = new HotCourseFragment();
                    Bundle bundle = new Bundle();
                    courseFragment.setArguments(bundle);

                    fragmentTransaction.add(R.id.content_layout, courseFragment, "tab0");
                } else {
                    fragmentTransaction.show(courseFragment);
                }
                fragmentTransaction.commit();
                break;
            case 1://
                setSelected(flHotVideo);
                if (videoFragment == null) {
                    videoFragment = new HotVideoFragment();
                    Bundle bundle = new Bundle();
                    videoFragment.setArguments(bundle);

                    fragmentTransaction.add(R.id.content_layout, videoFragment, "tab1");
                } else {
                    fragmentTransaction.show(videoFragment);
                }
                fragmentTransaction.commit();
                break;
            case 2://
                setSelected(flHotBook);
                if (bookFragment == null) {
                    bookFragment = new HotBookFragment();
                    Bundle bundle = new Bundle();
                    bookFragment.setArguments(bundle);

                    fragmentTransaction.add(R.id.content_layout, bookFragment, "tab2");
                } else {
                    fragmentTransaction.show(bookFragment);
                }

                fragmentTransaction.commit();
                break;
            case 3://活动
                setSelected(flHotActions);
                if (actionFragment == null) {
                    actionFragment = ActionFragment.newInstance(3);

                    fragmentTransaction.add(R.id.content_layout, actionFragment, "tab3");
                } else {
                    fragmentTransaction.show(actionFragment);
                }

                fragmentTransaction.commit();
                break;
        }
    }

    private void setSelected(View view) {
        flHotCourse.setSelected(false);
        flHotBook.setSelected(false);
        flHotVideo.setSelected(false);
        flHotActions.setSelected(false);
        view.setSelected(true);
    }

    /**
     * 隐藏所有的fragment
     *
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        if (videoFragment != null) {
            transaction.hide(videoFragment);
        }
        if (bookFragment != null) {
            transaction.hide(bookFragment);
        }
        if (courseFragment != null) {
            transaction.hide(courseFragment);
        }
        if (actionFragment != null) {
            transaction.hide(actionFragment);
        }
    }

    public void setShopCount(String count) {
        try {
            mShoppingNum = Integer.parseInt(count);
            tvShopCount.setText(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param type
     * 1减，2加
     */
    public void changeShopCount(int type){
        if(type==1){
            mShoppingNum--;
        }else if(type==2){
            mShoppingNum++;
        }
        tvShopCount.setText(String.valueOf(mShoppingNum));
    }
}
